{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c46d479d",
   "metadata": {},
   "source": [
    "# python基础入门3_选择语句&循环语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "838d46ec",
   "metadata": {},
   "source": [
    "## 双分支条件语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8e6e344f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个自然数：1\n",
      "1 is odd number.\n"
     ]
    }
   ],
   "source": [
    "#判断自然数的奇偶性\n",
    "num = input(\"请输入一个自然数：\")\n",
    "\n",
    "num = int(num)#string-->int\n",
    "\n",
    "if num%2==0:\n",
    "    print(\"{num} is even number.\".format(num=num))\n",
    "else:\n",
    "    print(\"{num} is odd number.\".format(num=num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9722b51a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#分支语句后面不要忘记写冒号！！！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "888f1816",
   "metadata": {},
   "source": [
    "## 多分支条件语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e87a5987",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "优\n"
     ]
    }
   ],
   "source": [
    "grade = \"A\"\n",
    "\n",
    "if grade==\"A\":\n",
    "    print(\"优\")\n",
    "elif grade==\"B\":\n",
    "    print(\"良\")\n",
    "else:\n",
    "    print(\"差\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c139a7f",
   "metadata": {},
   "source": [
    "## for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "473f5b59",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "18a526d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in lst:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c6a02642",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "849325d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,len(lst)):\n",
    "    print(lst[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d51d931e",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=list(range(1,11))#快速生成一个有序列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "77a07eea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "59ba5553",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "#break、continue\n",
    "\n",
    "#只输出奇数\n",
    "for i in lst:\n",
    "    if i%2==0:\n",
    "        continue\n",
    "    else:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "bdb1d57c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "#break、continue\n",
    "\n",
    "sum=0\n",
    "#只输出奇数\n",
    "for i in lst:\n",
    "    sum+=i;\n",
    "    if(sum==10):break\n",
    "    else:print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1910656",
   "metadata": {},
   "source": [
    "## while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "e345aa9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "while(i != 5):\n",
    "    print(i)\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "3ddc7b46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "i=1\n",
    "while i>0:\n",
    "    print(i)\n",
    "    if i==10:\n",
    "        break\n",
    "    else:\n",
    "        i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f44e2da0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92e4db1b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d582c275",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
